<?php

namespace App\Http\Controllers\WeChat;

use App\Http\Controllers\Controller;
use App\Models\User;
use EasyWeChat\Foundation\Application;
use Log;
/**
 * Class WeChatController
 * @package App\Http\Controllers\WeChat
 * 微信交互
 */
class WeChatController extends Controller
{
    private $wechat;//微信实例
    public function __construct(Application $wechat)
    {
        $this->wechat=$wechat;
    }

    /**
     * 处理微信的请求消息
     *
     * @return string
     */
    public function serve()
    {


        $this->wechat->server->setMessageHandler(function($message){
            log_arr($message);
            switch ($message->MsgType) {
                case 'event':
                    return $this->event($message);
                    break;
                case 'text':
                    return '';
                    break;
                case 'image':
                    return '';
                    break;
                case 'voice':
                    return '';
                    break;
                case 'video':
                    return '';
                    break;
                case 'location':
                    return '';
                    break;
                case 'link':
                    return '';
                    break;
                // ... 其它消息
                default:
                    return '';
                    break;
            }
        });
        return $this->wechat->server->serve();
    }

    /**
     * 微信事件处理中心
     * 微信事件转化成特定的关键词来处理
     * event可能的值：
     * subscribe : 关注公众号
     * unsubscribe : 取消关注公众号
     * scan : 扫描带参数二维码事件
     * location : 上报地理位置事件
     * click : 自定义菜单事件
     *
     * 2.将openid存入session
     */
    private function event($message)
    {
        switch ($message->Event) {
            //关注事件
            case 'subscribe':
                return $this->subscribeEvent($message);
                break;
            //取消关注
            case 'unsubscribe':
                $this->unsubscribeEvent($message);
                break;
            default:
                # code...
                break;
        }
    }

    /**
     * 名称：关注事件
     * 必要条件：用户关注公众号
     * 功能：处理用户关注公众号的逻辑
     * 任务(数据表操作)：
     * 1.拉取用户信息，存入数据库
     */
    private function subscribeEvent($message)
    {
        $openid=$message->FromUserName;
        if(!$openid){
            return ;
        }
        //从微信拉取数据生成新的用户数据
        $userFromWx=$this->wechat->user->get($openid);
        if(!$userFromWx){
            return ;
        }


        $user=User::where('openid',$openid)->first();
        if($user){
            //数据库中已存在此用户
            $user->subscribe=1;
            $user->nickname=deal_emoji($userFromWx->nickname,0);
            $user->gender=$userFromWx->sex;
            $user->head_img=$userFromWx->headimgurl;
            $user->city=$userFromWx->city;
            $user->province=$userFromWx->province;
            $user->country=$userFromWx->country;
            $user->subscribe_time=$userFromWx->subscribe_time;
            $user->remark=$userFromWx->remark;
            $user->save();
        }else{
            if($message->EventKey){
                //有带参数
                $spread_uid=str_replace('qrscene_','',$message->EventKey);
            }else{
                $spread_uid=0;
            }
            $dataIn=[
                'nickname'=>deal_emoji($userFromWx->nickname,0),
                'phone_number'=>'',
                'password'=>'',
                'gender'=>$userFromWx->sex,
                'balance'=>0,
                'openid'=>$openid,
                'campus_id'=>0,
                'university_id'=>0,
                'address'=>'',
                'head_img'=>$userFromWx->headimgurl,
                'subscribe'=>1,
                'city'=>$userFromWx->city,
                'province'=>$userFromWx->province,
                'country'=>$userFromWx->country,
                'subscribe_time'=>$userFromWx->subscribe_time,
                'remark'=>$userFromWx->remark,
                'manager'=>0,
                'share_code'=>uniqid(),
                'spread_uid'=>$spread_uid,
                'spread_status'=>0,
                'spread_qrcode'=>''
            ];
            User::create($dataIn);
        }
        return web_config('subscribe');
    }

    /**
     * 名称：关注事件
     * 必要条件：用户取消关注公众号
     * 功能：处理取消关注的逻辑
     * 任务：
     * 1.更新用户信息为未关注
     * 2.销毁session
     */
    private function unsubscribeEvent($message)
    {
        $openid=$message->FromUserName;
        if(!$openid){
            return ;
        }
        $user=User::where('openid',$openid)->first();
        if(!$user){
            return ;
        }
        $user->subscribe=0;
        $user->save();
    }

}
